package com.ruoyi.basebill.controller;

import java.math.BigDecimal;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.basebill.domain.BomCostcalcNewDomain;
import com.ruoyi.basebill.domain.BomCostcalcentry;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basebill.domain.BomCostcalc;
import com.ruoyi.basebill.service.IBomCostcalcService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * BOM成本测算Controller
 * 
 * @author ruoyi
 * @date 2024-09-06
 */
@RestController
@RequestMapping("/bomcostestimation/basebill/costcalc")
public class BomCostcalcController extends BaseController {
    @Autowired
    private IBomCostcalcService bomCostcalcService;

    /**
     * 查询BOM成本测算列表
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basebill:costcalc:list')")
    @GetMapping("/list")
    public TableDataInfo list(BomCostcalc bomCostcalc) {
//        startPage();
        List<BomCostcalc> list = bomCostcalcService.selectTBomCostcalcList(bomCostcalc,true);
        return getDataTable(list);
    }

    /**
     * 查询BOM成本测算列表
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basebill:costcalc:list')")
    @GetMapping("/list2")
    public TableDataInfo list2(BomCostcalc bomCostcalc) {
        startPage();
        List<BomCostcalcNewDomain> list = bomCostcalcService.selectBomCostcalcList3(bomCostcalc);
        return getDataTable(list);
    }

    /**
     * 导出BOM成本测算列表
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basebill:costcalc:export')")
    @Log(title = "BOM成本测算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BomCostcalc bomCostcalc) {
        String[] ids = bomCostcalc.getIds();
        List<BomCostcalc> list = new ArrayList<>();
        if(ids ==null|| ids.length==0){
            list = bomCostcalcService.selectTBomCostcalcList(bomCostcalc,false);
            for (int i = 0; i < list.size(); i++) {
                BomCostcalc element = bomCostcalcService.selectTBomCostcalcByFid(list.get(i).getFid());
                if (element!=null) {
                    List<BomCostcalcentry> bomCostcalcentryList = element.getBomCostcalcentryList();
                    Collections.sort(bomCostcalcentryList, new Comparator<BomCostcalcentry>() {
                        @Override
                        public int compare(BomCostcalcentry start, BomCostcalcentry end) {
                            if(start.getIndex()==null||end.getIndex()==null){
                                return 0;
                            }
                            String[] parts1 = start.getIndex().split("\\.");
                            String[] parts2 = end.getIndex().split("\\.");
                            int length1 = parts1.length;
                            int length2 = parts2.length;
                            int minLength = Math.min(length1, length2);
                            for (int i = 0; i < minLength; i++) {
                                int num1 = Integer.parseInt(parts1[i]);
                                int num2 = Integer.parseInt(parts2[i]);
                                if (num1 != num2) {
                                    return Integer.compare(num1, num2);
                                }
                            }
                            return Integer.compare(length1, length2);
                        }
                    });
                }
                list.set(i, element);
            }
        }else{
            for (int i = 0; i < ids.length; i++) {
                BomCostcalc element = bomCostcalcService.selectTBomCostcalcByFid(ids[i]);
                if (element!=null) {
                    List<BomCostcalcentry> bomCostcalcentryList = element.getBomCostcalcentryList();
                    Collections.sort(bomCostcalcentryList, new Comparator<BomCostcalcentry>() {
                        @Override
                        public int compare(BomCostcalcentry start, BomCostcalcentry end) {
                            if(start.getIndex()==null||end.getIndex()==null){
                                return 0;
                            }
                            String[] parts1 = start.getIndex().split("\\.");
                            String[] parts2 = end.getIndex().split("\\.");
                            int length1 = parts1.length;
                            int length2 = parts2.length;
                            int minLength = Math.min(length1, length2);
                            for (int i = 0; i < minLength; i++) {
                                int num1 = Integer.parseInt(parts1[i]);
                                int num2 = Integer.parseInt(parts2[i]);
                                if (num1 != num2) {
                                    return Integer.compare(num1, num2);
                                }
                            }
                            return Integer.compare(length1, length2);
                        }
                    });
                }
                list.add(element);
            }
        }
        ExcelUtil<BomCostcalc> util = new ExcelUtil<BomCostcalc>(BomCostcalc.class);
        util.exportExcel(response, list, "BOM成本测算数据");
    }

    /**
     * 获取BOM成本测算详细信息
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basebill:costcalc:query')")
    @GetMapping(value = "/{fid}")
    public AjaxResult getInfo(@PathVariable("fid") String fid) {
        return success(bomCostcalcService.selectTBomCostcalcByFid(fid));
    }

    /**
     * 新增BOM成本测算
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basebill:costcalc:add')")
    @Log(title = "BOM成本测算", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BomCostcalc bomCostcalc) {
        return toAjax(bomCostcalcService.insertTBomCostcalc(bomCostcalc));
    }

    /**
     * 修改BOM成本测算
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basebill:costcalc:edit')")
    @Log(title = "BOM成本测算", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BomCostcalc bomCostcalc) {
        return toAjax(bomCostcalcService.updateTBomCostcalc(bomCostcalc));
    }

    /**
     * 删除BOM成本测算
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basebill:costcalc:remove')")
    @Log(title = "BOM成本测算", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fids}")
    public AjaxResult remove(@PathVariable String[] fids) {
        return toAjax(bomCostcalcService.deleteTBomCostcalcByFids(fids));
    }

    /**
     * 获取Bom下级
     */
    @GetMapping("/getMaterialBomAllNodes/{id}/{longCode}")
    public HashMap getMaterialBomAllNodes(@PathVariable String id,@PathVariable("longCode") String longCode) {
        return bomCostcalcService.getMaterialBomAllNodes(id,longCode);
    }

    /**
     * 单据状态操作
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basebill:costcalc:aduit')")
    @GetMapping("/optionstatus/{ids}/{status}")
    public HashMap optionStatus(@PathVariable String[] ids, @PathVariable String status) {
        HashMap hashMap = bomCostcalcService.optionStatus(ids, status);
        return hashMap;
    }

    /**
     * 删除反写需求单
     */
    @GetMapping("/reverse/{ids}")
    public void reverse(@PathVariable String[] ids) {
        bomCostcalcService.reverse(ids);
    }

    /**
     * 根据正式Bom的数据查询系数
     */
    @Log(title = "BOM成本测算", businessType = BusinessType.UPDATE)
    @PutMapping("/getBomCoffe")
    public HashMap getBomCoffe(@RequestBody BomCostcalc bomCostcalc) {
        return bomCostcalcService.getBomCoffe(bomCostcalc);
    }

    /**
     * 移交产品库
     */
    @GetMapping("/moveProdect/{id}/{isMove}")
    public void moveProdect(@PathVariable String id, @PathVariable boolean isMove) {
        bomCostcalcService.moveProdect(id, isMove);
    }
}
